package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IPhoneLoginActivityModel;
import com.wgts.wgts.mvp.model.ISendSMSCodeModel;
import com.wgts.wgts.mvp.presenter.IPhoneLoginActivityPersenter;
import com.wgts.wgts.mvp.view.IPhoneLoginActivityView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.LoginRequest;
import com.wgts.wgts.netmodle.req.SmsCodeRequest;
import com.wgts.wgts.netmodle.resp.CurrentEntity;
import com.wgts.wgts.netmodle.resp.LoginEntity;
import com.wgts.wgts.rxjava.MVPResultSubscriber;
import com.wgts.wgts.tools.CountDownTime;
import com.wgts.wgts.tools.CountDownTimerListener;

public class PhoneLoginActivityPersenter implements IPhoneLoginActivityPersenter {

    private IPhoneLoginActivityView view;

    private IPhoneLoginActivityModel model;

    private ISendSMSCodeModel smsCodeModel;

    private CountDownTimerListener listener;

    public static final int SMS_ERROR = 011;

    public PhoneLoginActivityPersenter(IPhoneLoginActivityView view, IPhoneLoginActivityModel model, ISendSMSCodeModel smsCodeModel) {
        this.view = view;
        this.model = model;
        this.smsCodeModel = smsCodeModel;
    }

    @Override
    public void login(String userName, String passWord) {
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.account = userName;
        loginRequest.smsCode = passWord;
        model.login(loginRequest, new HttpResultSubscriber<LoginEntity>() {
            @Override
            public void onSuccess(LoginEntity loginEntity) {
                view.loginResult(loginEntity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void sendSms(String phone, String scene) {
        SmsCodeRequest smsCodeRequest = new SmsCodeRequest();
        smsCodeRequest.scene = scene;
        smsCodeRequest.phone = phone;
        smsCodeModel.sendSMS(smsCodeRequest, new HttpResultSubscriber<CurrentEntity>() {
            @Override
            public void onSuccess(CurrentEntity currentEntity) {
                view.sendSmsResult();
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(SMS_ERROR, msg);
            }
        });
    }

    @Override
    public void addCountDownTimeListener() {
        smsCodeModel.setCutDownTime(new MVPResultSubscriber<Integer>() {
            @Override
            public void netx(Integer integer) {
                if(integer < 1){
                    view.countDownTimeOver();
                    return;
                }
                view.countDowmTime(integer);
            }
        });
    }

    @Override
    public void removeCountDownTimeListener() {
        smsCodeModel.removeCutDownTime();
    }

    @Override
    public void phoneAffirm(String phone, String scene, String smsCode) {
        model.phoneAffirm(phone, scene, smsCode, new HttpResultSubscriber<CurrentEntity>() {
            @Override
            public void onSuccess(CurrentEntity currentEntity) {
                view.phoneResult();
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }


}
